package org.insia.smartposse.struts.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.IFreelanceDAO;
import org.insia.smartposse.metier.Freelance;
import org.insia.smartposse.utils.ActionUtils;
/**
 * Action for creating and modifying a freelance.
 * @author sok hout
 *
 */
public class CreateFreelanceAction extends DispatchAction {
	
	protected final Logger log = Logger.getLogger(CreateFreelanceAction.class);
	
    @Override
	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		Freelance freelance = new Freelance(-1L,"","","","",0,"","",0.0);
		request.setAttribute("freelance", freelance);
		request.setAttribute("messages", new String[]{});
		return mapping.findForward("show");
	}
    
	public ActionForward createFreelance(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
    	// initialisation de la liste des mesages d'erreurs
    	List<String> messages = new ArrayList<String>();
    	request.setAttribute("messages", messages);
    	
    	// recupération des données
    	Long id = ActionUtils.getLong(request, "id", -1L, messages, "freelance.error.id");
    	Integer gender = ActionUtils.getInt(request, "gender", 0, messages, "freelance.error.gender");
    	String firstName = ActionUtils.getTrimedString(request, "firstName","", messages, "freelance.error.firstName");
    	String lastName = ActionUtils.getTrimedString(request, "lastName","", messages, "freelance.error.lastName");
    	String login = ActionUtils.getTrimedString(request, "login","", messages, "freelance.error.login");
    	String password = ActionUtils.getTrimedString(request, "password","", messages, "freelance.error.password");
    	String email = ActionUtils.getEmail(request, "email","", messages, "freelance.error.email");
    	Double fee = ActionUtils.getDouble(request, "fee",0.0, messages, "freelance.error.fee");
    	String description = ActionUtils.getTrimedString(request, "description","", null,null);

    	Freelance freelance;
    	
    	// S'il y a eu des erreurs
    	if (messages.size() > 0) {
    		freelance = new Freelance(id.longValue(),login,password,firstName,lastName,gender.intValue(),email,description,fee.doubleValue());
    		request.setAttribute("freelance", freelance);
    		return mapping.findForward("failure");
    	}
    	
		IFreelanceDAO dao = DAOFactory.getFreelanceDAO();
    	// Si creation de nouveau freelance.
    	if (id == -1L) {
    		freelance = new Freelance(id.longValue(),login,password,firstName,lastName,gender.intValue(),email,description,fee.doubleValue());
    		dao.create(freelance);
    		request.setAttribute("msgTitleKey", "freelance.createSuccessTitle");
    		request.setAttribute("msgLabelKey", "freelance.createSuccessLabel");
    	} else {
    		//Sinon modification de freelance
    		freelance = dao.get(id);
    		freelance.setParams(id, login, password, firstName, lastName, gender, email, description, fee);
    		dao.update(freelance);
    		request.setAttribute("msgTitleKey", "freelance.updateSuccessTitle");
    		request.setAttribute("msgLabelKey", "freelance.updateSuccessLabel");
    	}
    	return mapping.findForward("messagePage");
    }
}